

package com.csz.model;

import com.csz.dao.Persistencia;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* @author Arthur F. Zanona */

public class Categoria extends Persistencia {
    private static final long serialVersionUID = 1L;

    private String nome;
    private Integer paiId;

    public Categoria() {
    }

    public Categoria(Integer id , String nome , Integer paiId) {
        this.id = id;
        this.nome = nome;
        this.paiId = paiId;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String catNome) {
        this.nome = catNome;
    }


    public Integer getCategoria() {
        return paiId;
    }

    public void setCategoriaPai(Integer categoriaPai) {
        this.paiId = categoriaPai;
    }

     @Override
    public void salvar() {
        String namedQuery = this.getId() == null ? Inserir : Atualizar ;
        //----- params aqui
        List<Object> params = new ArrayList<Object>();
        //---- fim params
        if ( this.getId() != null ) params.add( this.getId() );
        PreparedStatement ps = createStatementNamedQuery(namedQuery, params);
        try {
            ps.execute();
        } catch (SQLException ex) {}
    }

    public static Object consultar(Integer id) {
        List<Object> l = Categoria.consultarNamedQuery(ConsultarPorId, Arrays.asList(new Object[]{id}));
        return l == null ? null : l.get(0);
    }

    @Override
    public void remover() {
        PreparedStatement ps = createStatementNamedQuery(Remover, Arrays.asList( new Object[] { this.getId() } ));
        try {
            ps.execute();
        } catch (SQLException ex) {}
    }
    
   public static List<Object> consultar() {
        return Categoria.consultarNamedQuery(ConsultarTodos, Arrays.asList(new Object[]{}));
   }

   public static List<Object> consultarNamedQuery(String namedQuery, List params){
        PreparedStatement ps = createStatementNamedQuery(namedQuery, params);
        List<Object> lista = new ArrayList<Object>();
        try {
            ResultSet rs = ps.executeQuery();
            while ( rs.next() ) {
                Categoria c = new Categoria( rs.getInt("cat_id")  , rs.getString("cat_nome") , rs.getInt("cat_cat_id") );
                lista.add(c);
            }
        }
        catch (SQLException ex) {
        }
        return lista;
    }

       // QUERY SQL --------------------------------------------------------------------------------------------------------------
    public static final String ConsultarTodos = "SELECT cat_id,cat_nome,cat_cat_id FROM categoria WHERE cat_cat_id is null";
    public static final String ConsultarPorPai =  "SELECT cat_id,cat_nome,cat_cat_id FROM categoria c WHERE c.cat_cat_id = ?";
    public static final String ConsultarPorId = "SELECT cat_id,cat_nome,cat_cat_id FROM categoria WHERE cat_id = ?";
    private static final String Remover = "DELETE FROM .. WHERE .._id = ?";
    private static final String Atualizar = "UPDATE .. SET .. ";
    private static final String Inserir = "INSERT INTO .. (..) VALUES (?,?,..)";
    // FIM QUERY SQL --------------------------------------------------------------------------------------------------------------

}
